<?php
	namespace Common\Logic;
	use Think\Controller;
	class ApiPlantformLogic extends Controller{
		var $errorMsg = '';
		
		/*
		* Function:addGuanYiOrders
		* Description:管易平台主动推送订单
		* @param: $order_id string 订单号
		*/
		public function addGuanYiOrders($order_id = ''){
		    if(empty($order_id)){
		        $this->errorMsg = '缺少参数';
		        return false;
		    }
		    if(!$orderInfo = M('orders')->where(['order_id'=>$order_id])->find()){
		        $this->errorMsg = '订单不存在';
		        return false;
		    }
		    if(intval($orderInfo['status'])!==2){
		        $this->errorMsg = '订单状态不对';
		        return false;
		    }
		    if(!$addressInfo = M('order_address')->where(['order_id'=>$order_id])->find()){
		        $this->errorMsg = '缺少用户地址信息';
		        return false;
		    }
		    $orderItems = M('order_list')->where(['order_id'=>$order_id])->select();
		    if(count($orderItems)==0){
		        $this->errorMsg = '订单不存在明细';
		        return false;
		    }
			$data = [];
            $data['appkey'] = C('guanyi_config.appkey');
            $data['sessionkey'] = C('guanyi_config.sessionkey');
            $data['method'] = C('guanyi_config.method_add_order');
            $data['order_type_code'] = 'Sales';
            $data['warehouse_code'] = C('guanyi_config.warehouse_code');//仓库代码
            $data['shop_code'] = C('guanyi_config.shop_code');//店铺代码
            $data['express_code']= '';//物流公司-中通直连
            $data['platform_code'] = $order_id;//平台单号
            $data['deal_datetime'] = $orderInfo['addtime'];//拍单时间
            $data['pay_datetime'] = $orderInfo['pay_time'];//支付时间
            $data['tag_code'] = 'wx_trido';//订单标记
            $data['vip_code'] = $addressInfo['ship_mobile'];//会员代码
            $data['buyer_memo'] = $orderInfo['order_desc'];//买家留言
            $data['seller_memo'] = '';//卖家备注
            $data['seller_memo_late'] = '';//卖家二次备注
            $data['receiver_name'] = $addressInfo['ship_name'];//收货人
            $data['receiver_phone'] = '';//收货人电话
            $data['receiver_mobile'] = $addressInfo['ship_mobile'];//收货人手机
            $data['receiver_address'] = $addressInfo['address'];//收货人地址
            $data['receiver_province'] = $addressInfo['province'];//收货人省
            $data['receiver_city'] = $addressInfo['city'];//收货人市
            $data['receiver_district'] = $addressInfo['area'];//收货人区
            $details = [];
            if(is_array($orderItems)&&count($orderItems)>0){
                foreach($orderItems as $key=>$val){
                    $goodsInfo = M('goods')->where(['id'=>$val['goods_id']])->find();
                    $goodsSpecInfo = M('goods_list')->where(['id'=>$val['goods_list_id']])->find();
                    $details[] = [
                        'item_code' => $goodsSpecInfo['goods_sku'],//商品代码 
                        'price' => $val['price'],//实际单价
                        'qty' => $val['nums'],//商品数量
                    ];
                }
            }
            $data['details'] = $details;
            $payments = [];
            $payments[] = array(
                'pay_type_code' => 'wxpay',//支付方式
                'payment' => floatval($orderInfo['pay_price']),//支付金额
                'pay_code' => $orderInfo['pay_no'],//交易号
                'paytime' => strtotime($orderInfo['pay_time'])//支付时间
            );
            $data['payments'] = $payments;
            $data['sign'] = $this->guanYiDataSign($data,C('guanyi_config.secret'));
			$returnData = guanyi_curl(C('guanyi_config.api_url'),$data);
			$returnData = json_decode($returnData,true);
			return $returnData;
		}
		
		private function guanYiDataSign($data,$secret){
			if (empty($data)) {
                return "";
            }
            unset($data['sign']); //可选，具体看传参
            $data = json_encode_ch($data);
            $sign = strtoupper(md5($secret.$data.$secret));
            return $sign;
		}
		
		private function getExpressCode($delivery_name = ''){
			switch($delivery_name){
				case 'EMS':
					$code = 'EMS';
					break;
				case '顺丰速运':
					$code = 'SF';
					break;
				case '中通速递':
					$code = 'ZTO';
					break;
				case '圆通速递':
					$code = 'YTO';
					break;
				case '北仑军通':
					$code = 'YTO';
					break;
				case '韵达快递':
					$code = 'YUNDA';
					break;
				case '百世物流':
					$code = 'BSWL';
					break;
				default:
					break;
			}
			return $code;
		}
		
		/*
		* Function:get:	getGuanYiOrders
		* Description:	查询订单信息
		* @param:		$info array 数组
		*/
		public function getGuanYiOrders($order_id = ''){
		    if(empty($order_id)){
		        $this->errorMsg = '缺少参数';
		        return false;
		    }
		    if(!$orderInfo = M('orders')->where(['order_id'=>$order_id])->find()){
		        $this->errorMsg = '订单不存在';
		        return false;
		    }
		    if(intval($orderInfo['sync_status'])!==1){
		        $this->errorMsg = '订单状态不对';
		        return false;
		    }
			$data = array();
            $data['appkey'] = C('guanyi_config.appkey');
            $data['sessionkey'] = C('guanyi_config.sessionkey');
            $data['method'] = C('guanyi_config.method_get_order');
            // $data['page_no'] = '1';
            // $data['page_size'] = '10';
            // $data['start_date'] = date('Y-m-d 00:00:00',time());
            // $data['end_date'] = date('Y-m-d H:i:s',strtotime("+1 day"));
            // $data['date_type'] = 0;//时间类型 0:创建时间
            // $data['order_state'] = 0;//订单类型 0:全部
            $data['warehouse_code'] = C('guanyi_config.warehouse_code');//仓库代码
            $data['platform_code'] = $order_id;//订单号
            $data['sign'] = $this->guanYiDataSign($data,C('guanyi_config.secret'));
			$returnData = guanyi_curl(C('guanyi_config.api_url'),$data);
			$returnData = json_decode($returnData,true);
			return $returnData;
		}
		
		function get_error(){
			return $this->errorMsg;
		}
	}
?>